package imp

import (
	sqlc "bgs/db/sqlc"
	brandBankCardGrpc "bgs/grpc/gen/service/brand_bank_card"
	"bgs/grpc/gen/shared/message"
	"bgs/util"
	"database/sql"
	brandService "bgs/service/brand"
	"context"
)

/****************************************************************************************/
/*                               brand_bank_card entity of helper                           */
/****************************************************************************************/

func transformBrandBankCardEntityToPb(o sqlc.BrandBankCard) *brandBankCardGrpc.BrandBankCardEntity {
	return &brandBankCardGrpc.BrandBankCardEntity{
		Id:              o.ID,
		BrandId:         o.BrandID,
		UserId:          o.UserID,
		AccountType:     o.AccountType,
		AccountBank:     o.AccountBank,
		BankAddressCode: o.BankAddressCode,
		BankName:        o.BankName,
		AccountNumber:   o.AccountNumber,
		VerifyResult:    o.VerifyResult,
	}
}

/****************************************************************************************/
/*                               brand_bank_card entity of cmd                              */
/****************************************************************************************/

// UpsertBrandBankCard 创建品牌银行卡
func (s *BrandAPIService) UpsertBrandBankCard(ctx context.Context, req *brandBankCardGrpc.UpsertBrandBankCardRequest) (res *brandBankCardGrpc.UpsertBrandBankCardResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("brand_bank_cardImp->UpsertBrandBankCard:%v", req)
	res = &brandBankCardGrpc.UpsertBrandBankCardResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}

	var brandBankCard sqlc.BrandBankCard
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		brandBankCard, err = s.brandService.SetBankCardForSettlement(ctx, q, brandService.SetBrandBankCardForSettlementParam{
			UpsertPtr: &sqlc.UpsertBrandBankCardParams{
				BrandID:         req.BrandId,
				UserID:          req.UserId,
				AccountType:     req.AccountType,
				AccountBank:     req.AccountBank,
				BankAddressCode: req.BankAddressCode,
				BankName:        req.BankName,
				AccountNumber:   req.AccountNumber,
			},
		})
		if err != nil {
			return err
		}
		return
	})

	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.BrandBankCard = transformBrandBankCardEntityToPb(brandBankCard)
	return
}

/****************************************************************************************/
/*                               brand_bank_card entity of query                        */
/****************************************************************************************/

// GetBrandBankCard 获取单个品牌提现银行卡
func (s *BrandAPIService) GetBrandBankCard(ctx context.Context, req *brandBankCardGrpc.GetBrandBankCardRequest) (res *brandBankCardGrpc.GetBrandBankCardResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("brand_bank_cardImp->GetBrandBankCard:%v", req)
	res = &brandBankCardGrpc.GetBrandBankCardResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	brandFollow, err := q.GetBrandBankCard(ctx, req.Id)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.BrandBankCard = transformBrandBankCardEntityToPb(brandFollow)
	return
}

// GetLatestBrandBankCardByBrandID 根据品牌id获取最新的一个品牌提现银行卡
func (s *BrandAPIService) GetLatestBrandBankCardByBrandID(ctx context.Context, req *brandBankCardGrpc.GetLatestBrandBankCardByBrandIDRequest) (res *brandBankCardGrpc.GetLatestBrandBankCardByBrandIDResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("brand_bank_cardImp->GetLatestBrandBankCardByBrandID:%v", req)
	res = &brandBankCardGrpc.GetLatestBrandBankCardByBrandIDResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	brandFollow, sqlErr := q.GetLatestBrandBankCardByBrandID(ctx, req.BrandId)
	if sqlErr != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    util.Int32IfThenElse(sql.ErrNoRows == sqlErr, 404, 400),
			Msg:     sqlErr.Error(),
		}
		return
	}
	res.BrandBankCard = transformBrandBankCardEntityToPb(brandFollow)
	return
}